package com.app.controller;

import com.app.model.ProjectModel;
import com.app.service.ProjectService;
import com.app.util.CommonHelp;
import com.app.util.DateMaskingUtils;
import com.app.util.Result;
import com.app.vo.ProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//创业项目
@RestController
@Controller
@RequestMapping("/jiuquan/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    //查询列表
    @GetMapping("/getProjectList")
    public Result getProjectList(@RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit, String projectName, String startTime, String endTime, Double investment) {
        ProjectVo projectList = projectService.getProjectList(page, limit, projectName, startTime, endTime, investment);
        return Result.success(projectList);
    }

    //详情
    @GetMapping("/getProjectById")
    public Result getProjectById(Integer id) {
        ProjectModel project = projectService.getById(id);
        String phone = project.getPhone();
        String phoneMi = DateMaskingUtils.mobileEncrypt(phone);
        project.setPhoneMi(phoneMi);
        return Result.success(project);
    }

    //统计拨打电话成功
    @PostMapping("/addProjectCount")
    public Result addProjectNumber(@RequestBody ProjectModel projectModel) {
        ProjectModel project = projectService.getById(projectModel.getId());
        Integer contactCount = project.getContactCount();
        contactCount = contactCount + 1;
        project.setContactCount(contactCount);
        projectService.updateById(project);
        return Result.success("联系成功");
    }

    //新增和修改
    @PostMapping("/addAndUpdateProject")
    public Result addAndUpdateProject(@RequestBody ProjectModel projectModel) {
        ProjectModel project = projectService.getById(projectModel.getId());
        if (project != null) {
            projectModel.setUpdateTime(CommonHelp.getNowTimestamp());
            projectService.updateById(projectModel);
            return Result.success("修改成功");
        } else {
            projectModel.setCreateTime(CommonHelp.getNowTimestamp());
            projectService.save(projectModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delProject")
    public Result delProject(Integer id) {
        projectService.removeById(id);
        return Result.success("删除成功");
    }
}
